package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"time"
)

var timeFormat = "2006-01-02 15:04:05"
var restartTimeLimit = 1000

func main() {
	lf, err := os.OpenFile("log.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer lf.Close()

	// log
	l := log.New(lf, "", os.O_APPEND)

	p, _ := filepath.Abs(os.Args[1])
	for i := 0; i < restartTimeLimit; i++ {
		cmd := exec.Command(p)
		cmd.Stdout = os.Stdout
		err = cmd.Start()
		if err != nil {
			l.Printf("%s process start error : %s", time.Now().Format(timeFormat), err.Error())
			time.Sleep(time.Second)
			continue
		}
		l.Printf("%s process start", time.Now().Format(timeFormat))
		err = cmd.Wait()
		if err != nil {
			l.Printf("%s process end error: %s", time.Now().Format(timeFormat), err.Error())
		} else {
			l.Printf("%s process end", time.Now().Format(timeFormat))
		}
		time.Sleep(time.Millisecond)
	}

}
